package interceptors

import (
	"time"

	"github.com/gin-contrib/cors"
	"github.com/gin-gonic/gin"
)

// CORS 跨域请求中间件
func CORS() gin.HandlerFunc {
	origins := []string{"*"}
	//domains := viper.GetStringSlice("cors.domain")
	//if len(domains) > 0 {
	//	origins = append(origins, domains...)
	//}
	return cors.New(cors.Config{
		AllowOrigins:     origins,
		AllowMethods:     []string{"OPTIONS", "GET", "POST", "PUT", "DELETE", "PATCH"},
		AllowHeaders:     []string{"Access-Control-Allow-Origin", "Content-Type", "Accept-Language", "Authorization", "x-platform", "x-device-id", "x-api-key", "x-api-signature", "x-timestamp", "Origin", "x-zone"},
		AllowCredentials: true,
		MaxAge:           time.Second * time.Duration(7200),
	})
}
